﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Text;

namespace Runda.CMS.Portal.Report
{
    public partial class SalesRpt_102a : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2012; i <= 2015; i++)
                {
                    ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                DataTable dt = new BudgetBll().Companylist(base.CurrentUserID.ToString());
                ddlCompany.DataSource = dt;
                ddlCompany.DataTextField = "Departmentname";
                ddlCompany.DataValueField = "Departmentname";
                ddlCompany.DataBind();
                ddlCompany.Items.Add(new ListItem("--不限--", "0"));
                ddlCompany.SelectedValue = "0";

                

                string sqlWhere = getWhereClause();
                LoadContractTrack(sqlWhere);

            }
        }

        private void setdrData(ref DataRow drnew, DataRow drsource)
        {
            drnew["rdBudgetAmount"] = drsource["rdBudgetAmount"];
            drnew["rdSaleAmount"] = drsource["rdSaleAmount"];
            drnew["rdRate"] = drsource["rdRate"];
            drnew["kxBudgetAmount"] = drsource["kxBudgetAmount"];
            drnew["kxSaleAmount"] = drsource["kxSaleAmount"];
            drnew["kxRate"] = drsource["kxRate"];
            drnew["yxBudgetAmount"] = drsource["yxBudgetAmount"];
            drnew["yxSaleAmount"] = drsource["yxSaleAmount"];
            drnew["yxRate"] = drsource["yxRate"];
            drnew["hzBudgetAmount"] = drsource["hzBudgetAmount"];
            drnew["hzSaleAmount"] = drsource["hzSaleAmount"];
            drnew["hzRate"] = drsource["hzRate"];
            drnew["hcBudgetAmount"] = drsource["hcBudgetAmount"];
            drnew["hcSaleAmount"] = drsource["hcSaleAmount"];
            drnew["hcRate"] = drsource["hcRate"];
            drnew["TotalBudgetAmount"] = drsource["TotalBudgetAmount"];
            drnew["TotalSaleAmount"] = drsource["TotalSaleAmount"];
            drnew["TotalRate"] = drsource["TotalRate"];
        }
        private void LoadContractTrack(string sqlWhere)
        {
            //set reference
            ReportViewer ReportViewer = rvReporting;

            //get rdlc path
            string strRootPath = this.Server.MapPath(this.Request.ApplicationPath);
            string strFilePath = strRootPath + "Report\\rdlc\\SalesRpt_102a.rdlc";

            strFilePath = "report/rdlc/SalesRpt_102a.rdlc";

            //set report viewer property
            ReportViewer.SizeToReportContent = true;
            ReportViewer.ZoomMode = ZoomMode.Percent;
            ReportViewer.AsyncRendering = false;
            
            //set report viewer work mode
            ReportViewer.ProcessingMode = ProcessingMode.Local;
            LocalReport lp = ReportViewer.LocalReport;
            lp.ReportPath = strFilePath;

            //get dataset  
            BillBll bll = new BillBll();
            DataSet ds;
            if (rbtype.SelectedValue == "1")
            {
                ds = bll.SalesRpt_102a(sqlWhere, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);
            }
            else
            { ds = bll.SalesRpt_102b(sqlWhere, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue); }
            ds.DataSetName = "Budget2SaleDataSet_dbo_vw_Budget2Sale_ByProduct";
            //set data set
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "Budget2SaleDataSet_dbo_vw_Budget2Sale_ByProduct";

            DataTable dt = ds.Tables[0];
            DataTable dttmp;
            DataRow dr;
            DataRow drtmp;

            if (rbtype.SelectedValue == "1")
            {
                dr = dt.NewRow();
                dttmp = bll.sumSalesRpt_102a(sqlWhere, "", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);
                if (dttmp.Rows.Count > 0)
                {
                    drtmp = dttmp.Rows[0];
                    dr["FactoryName"] = "900合计：";
                    setdrData(ref dr, drtmp);
                    dt.Rows.Add(dr);
                }
                dr = dt.NewRow();
                dttmp = bll.sumSalesRpt_102a(sqlWhere, " and isOwner='1'", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);
                if (dttmp.Rows.Count > 0)
                {
                    drtmp = dttmp.Rows[0];
                    dr["FactoryName"] = "901自产产品小计：";
                    setdrData(ref dr, drtmp);
                    dt.Rows.Add(dr);
                }
                dr = dt.NewRow();
                dttmp = bll.sumSalesRpt_102a(sqlWhere, " and isOwner='0'", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);
                if (dttmp.Rows.Count>0)
                {
                    drtmp = dttmp.Rows[0];
                    dr["FactoryName"] = "902代理产品小计：";
                    setdrData(ref dr, drtmp);
                    dt.Rows.Add(dr);
                }
            }
            else
            {
                dr = dt.NewRow();
                dttmp = bll.sumSalesRpt_102b(sqlWhere, "", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue, ddlBudgetYear.SelectedValue);
                if (dttmp.Rows.Count > 0)
                {
                    drtmp = dttmp.Rows[0];
                    dr["FactoryName"] = "990合计：";
                    setdrData(ref dr, drtmp);
                    dt.Rows.Add(dr);
                }
                    dr = dt.NewRow();
                    dttmp = bll.sumSalesRpt_102b(sqlWhere, " and isOwner='1'", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue, ddlBudgetYear.SelectedValue);
                    if (dttmp.Rows.Count > 0)
                    {
                        drtmp = dttmp.Rows[0];
                        dr["FactoryName"] = "991自产产品小计：";
                        setdrData(ref dr, drtmp);
                        dt.Rows.Add(dr);
                    }
                

                dr = dt.NewRow();
                dttmp = bll.sumSalesRpt_102b(sqlWhere, " and isOwner='0'", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue, ddlBudgetYear.SelectedValue);
                if (dttmp.Rows.Count > 0)
                {
                    drtmp = dttmp.Rows[0];
                    dr["FactoryName"] = "992代理产品小计：";
                    setdrData(ref dr, drtmp);
                    dt.Rows.Add(dr);
                }
            }
            rds.Value = dt;
            lp.DataSources.Clear();
            lp.DataSources.Add(rds);

            ReportViewer.LocalReport.Refresh();
            
        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();

            //if (base.CurrentUserEntity.UserName == "郭海涛"
            //    || base.CurrentUserEntity.UserName == "史莹"
            //    || base.CurrentUserEntity.UserName == "王静"
            //    || base.CurrentUserEntity.UserName == "顾悦"
            //    || base.CurrentUserEntity.UserName == "宋欣"
            //    || base.CurrentUserEntity.UserName == "徐雪峰"
            //    || base.CurrentUserEntity.UserName == "胡日成"
            //    || base.CurrentUserEntity.UserName == "崔丽丽"
            //    || base.CurrentUserEntity.UserName == "官俊"
            //    || base.CurrentUserEntity.UserName == "李娜"
            //    || base.CurrentUserEntity.UserName == "张佳锦"
            //    )
            //{
            //    sb.Append(" and companyname in(").Append("select DepartmentName from cms_sys_Department where iscompany = 1 and DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
            //}
            //else
            //{
                sb.Append(" and ((companyname in(").Append("select DepartmentName from cms_sys_Department where iscompany = 1 and DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
                sb.Append(" and CategoryCode in(").Append("select categoryCode from Cms_Ctrt_InstrumentCategory where categoryGUID in(select categoryGUID from Cms_sys_UserProductCategory where UserID ='").Append(base.CurrentUserID.ToString()).Append("')))");
                sb.Append(" or sales ='").Append(base.CurrentUserEntity.UserName.ToString()).Append("'");
                sb.Append(" or sales in(").Append("select username from vw_sysUser where DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("')))");
                
            //}
            if (ddlCompany.SelectedValue != "0")
            {
                sb.Append(" and companyname = '" + ddlCompany.SelectedValue.ToString() + "'");
            }
            

            return sb.ToString();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string sqlWhere = getWhereClause();
            LoadContractTrack(sqlWhere);
        }

        
    }
}
